const { Comment } = require('../models/comments.js')
const sendMsg = require('../utils').SendMsg
const moment = require('moment');
/**
 * 添加反馈
 * parmas
 *  usertoken: 'String',
 *  context: 'String',
 *	comment_img: 'String'
 *	comment_time: 'String'
 */
exports.addComment = async (ctx, next) => {
	let params = {
		usertoken:  ctx.request.body.usertoken,
		context: ctx.request.body.context,
		comment_img: ctx.request.body.comment_img,
		comment_time: moment().format('YYYY-MM-DD HH:mm:ss')
	}
	let data = await Comment.create(params);

	if (!data) {
		ctx.body = SendMsg(-20010)
	} else {
		ctx.body = {
			status: 0,
			result: {
				msg: 'success'
			}
		}
	}
}
/**
 * 我的反馈
 * @param usertoken: 'String'
 */
exports.getCommentByUserToken = async (ctx, next) => {
	let params = {
		usertoken: ctx.request.body.usertoken
	}
	let [data, count] = await Promise.all([
			Comment.find({usertoken: params.usertoken}),
			Comment.find({usertoken: params.usertoken}).count()
		])
	
	if (data) {
		ctx.body = {
			status: 0,
			result: {
				list: data,
				totals: count
			}
		}
	}
}

/**
 * 删除反馈
 * @param commentid: 'String'
 */
exports.delCommentById = async (ctx, next) => {
	let params = {
		usertoken: ctx.request.body.usertoken,
		recommentid: ctx.request.body.recommentid
	}
	let check = await Comment.findOne({_id: params.recommentid})

	if (!check) {
		
	} else  {
		let data = await Comment.remove({_id: params.recommentid})
		if (!data) {

		} else　{
			ctx.body = {
				status: 0,
				result: {
					msg: "删除成功"
				}
			}
		}
	}
}
